home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / U-Z / VideoToolBox Folder / VideoToolboxSources / PrintfExit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-27  |  2.9 KB  |  87 lines  |  [TEXT/KAHL]

  1. /* 
  2. PrintfExit.c
  3.  
  4. PrintfExit() is normally equivalent to calling printf and exit.
  5.  
  6. Require(quickDrawVersion) checks that the computer is providing the environment
  7. that your program needs, and fails gracefully if not.
  8.  
  9. Lots of VideoToolbox routines, when they find a really grievous error, print out
  10. a message and exit. Having one routine that does both makes the code slightly
  11. neater, since the if statement then doesn't need braces. Furthermore, both of
  12. these functions, printf, and exit are liable to break in foreign environments,
  13. e.g when running as a MEX resource under MatLab. Now the problem is confined to
  14. this file, where it can be handled by conditional compilation.
  15.  
  16. Since PrintfExit is called only when we're near death, it seems prudent to make sure
  17. there's enough stack space before calling printf, since it crashes if there's
  18. less than about 4500 byts of stack. Note that StackGrow() moves memory.
  19.  
  20. I've replaced all calls to exit() in the entire VideoToolbox by calls to
  21. PrintfExit().
  22.  
  23. HISTORY:
  24. 2/20/93    dgp    Wrote it based on conversation with David Brainard.
  25. */
  26. #include "VideoToolbox.h"    // StackGrow()
  27. #include <stdarg.h>            // for variable-number-of-argument macros
  28. #include "mc68881.h"
  29.  
  30. int PrintfExit(char *format,...)
  31. {
  32.     va_list args;
  33.     int i;
  34.     long value=0;
  35.   
  36.     // The main program may have changed the current device. Let's
  37.     // restore the main device before doing the printf.
  38.     Gestalt(gestaltQuickdrawVersion,&value);
  39.     if(value>=gestalt8BitQD)SetGDevice(GetMainDevice());
  40.     
  41.     if(StackSpace()<6000)StackGrow(6000-StackSpace());
  42.     #ifndef MATLAB
  43.         // printf crashes if there's less than about 4500 bytes of stack space
  44.         if(StackSpace()>5000){
  45.             va_start(args,format);
  46.             i=vfprintf(stdout,format,args);
  47.             va_end(args);
  48.         }else SysBeep(20);
  49.         exit(1);
  50.     #else
  51.     {
  52.         char s[512];
  53.         
  54.         va_start(args,format);
  55.         i=vsprintf(s,format,args);
  56.         va_end(args);
  57.         mex_error(s);    // Ask MatLab to report the error.
  58.     }
  59.     #endif
  60. }
  61.  
  62. void Require(long quickDrawVersion)
  63. // Call this at the beginning of your main program, so that your program
  64. // will fail gracefully, instead of crashing, when someone runs it
  65. // on a computer that lacks what your program needs.
  66. // The fpu and cpu tests automatically track your current compiler settings.
  67. {
  68.     long value;
  69.     OSErr error;
  70.     
  71.     error=Gestalt(gestaltFPUType,&value);
  72.     if(error)PrintfExit("Sorry, I require Gestalt(). Your System is too old!\n");
  73.     if(mc68881 && value==0)
  74.         PrintfExit("Sorry. I've been compiled to use a floating point chip,"
  75.             " and you don't have one.\n");
  76.     error=Gestalt(gestaltProcessorType,&value);
  77.     if(mc68020 && value<gestalt68020)
  78.         PrintfExit("Sorry. I've been compiled to use a 68020 processor (or better),"
  79.             " and you don't have one.\n");
  80.     Gestalt(gestaltQuickdrawVersion,&value);
  81.     if(value<quickDrawVersion)switch(quickDrawVersion){
  82.         case gestalt8BitQD:
  83.             PrintfExit("Sorry. This program requires Color QuickDraw.\n");
  84.         default:
  85.             PrintfExit("Sorry. This program requires 32-bit QuickDraw.\n");
  86.     }
  87. }